package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 套餐 前端控制器
 * </p>
 *
 * @author 黑马程序员
 * @since 2022-06-06
 */
@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {
    @Autowired
    private SetmealService setmealService;

    @Autowired
    private SetmealDishService setmealDishService;

    @Autowired
    private CategoryService categoryService;

    /**
     * 添加数据
     * @param setmealDto
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto) {

        setmealService.saveBysetmealDto(setmealDto);
        return R.success("添加成功");
    }

    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> pageSelect(int page, int pageSize, String name) {


        log.info("page = {},pageSize = {}", page, pageSize);

//        BeanUtils.copyProperties(dish,pageInfo);
        Page page1 = setmealService.selectBypage(page, pageSize, name);

        return R.success(page1);
    }

    /**
     * 修改之前进行数据回显
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<SetmealDto> updateBydto(@PathVariable Long id) {
        SetmealDto setmealByid = setmealService.getSetmealByid(id);
        return R.success(setmealByid);
    }

    @PutMapping
    public R<String> updateBydto(@RequestBody SetmealDto setmealDto) {
        setmealService.updateBysetmealDto(setmealDto);
        return R.success("修改成功");

    }

    /**
     * 修改状态
     * @param ids
     * @return
     */
    @PostMapping("/status/0")
    public R<String> updateByStatus0(@RequestParam List<Long> ids) {

        List<Setmeal> list = setmealService.listByIds(ids);
        for (Setmeal setmeal : list) {
            setmeal.setStatus(0);
        }
        setmealService.saveOrUpdateBatch(list);
        return R.success("状态修改成功");
    }

    @PostMapping("/status/1")
    public R<String> updateByStatus1(@RequestParam List<Long> ids) {

        List<Setmeal> list = setmealService.listByIds(ids);
        for (Setmeal setmeal : list) {
            setmeal.setStatus(1);
        }
        setmealService.saveOrUpdateBatch(list);
        return R.success("状态修改成功");
    }

    /**
     * 通过id集合批量删除
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> deleteById(@RequestParam List <Long> ids) {
        setmealService.deleteByIds(ids);
        return R.success("删除成功");
    }

    /**
     * 手机端增强使用dto进行套餐回显
     * @param setmeal
     * @return
     */
    @GetMapping("/list")
    public R<List<SetmealDto>> selectByCategoryId( Setmeal setmeal){

        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper();
        qw.eq(Setmeal::getCategoryId,setmeal.getCategoryId());
        qw.eq(Setmeal::getStatus,1);
        qw.orderByAsc(Setmeal::getUpdateTime);
        List<Setmeal> setmeallist = setmealService.list(qw);
        //把setmeal_id收集到一个list集合中
        List<Long> setmealIdList = setmeallist.stream().map(item -> item.getId()).collect(Collectors.toList());
        //通过id查询setmealdish
        LambdaQueryWrapper<SetmealDish> qw2 = new LambdaQueryWrapper();
        qw2.in(SetmealDish::getSetmealId,setmealIdList);
        qw2.orderByAsc(SetmealDish::getUpdateTime);
        List<SetmealDish> setmealDishlist = setmealDishService.list(qw2);


        //循环遍历给setmealDto赋值
        List<SetmealDto> setmealDtolist =new ArrayList<>();
        for (Setmeal setmeal1 : setmeallist) {
            SetmealDto setmealDto =new SetmealDto();
            BeanUtils.copyProperties(setmeal1,setmealDto);
            Long id = setmealDto.getId();
            //通过id获取到的setmealdish中的数据
            List<SetmealDish> collect = setmealDishlist.stream().filter(setmeldish -> id.equals(setmeldish.getSetmealId())).collect(Collectors.toList());
            setmealDto.setSetmealDishes(collect);
            setmealDtolist.add(setmealDto);
        }
        return R.success(setmealDtolist);
    }

    /**
     *套餐数据回显
     * @param id
     * @return
     */
    @GetMapping("/dish/{id}")
    public R<List<SetmealDish>> selectBySetmealId(@PathVariable Long id){
        LambdaQueryWrapper<SetmealDish> qw =new LambdaQueryWrapper<>();
        qw.eq(SetmealDish::getSetmealId,id);
        qw.orderByAsc(SetmealDish::getUpdateTime);
        List<SetmealDish> list = setmealDishService.list(qw);
        Setmeal byId = setmealService.getById(id);
        String image = byId.getImage();
        for (SetmealDish setmealDish : list) {
            setmealDish.setImage(image);
        }

//        LambdaQueryWrapper<Setmeal> qw2 =new LambdaQueryWrapper<>();
//        qw2.eq(Setmeal::getId,id);
//        qw2.orderByAsc(Setmeal::getUpdateTime);
//        Setmeal byId = setmealService.getById(id);
//        String image = byId.getImage();
//        SetmealDto setmealDto =new SetmealDto();
//        setmealDto.setImage(image);
//        setmealDto.setSetmealDishes(list);
//        setmealDtoList.add(setmealDto);
        return  R.success(list);
    }
}

